/*     */ package com.cr.framework.web.service;
/*     */ 
/*     */ import java.beans.BeanInfo;
/*     */ import java.beans.Introspector;
/*     */ import java.beans.PropertyDescriptor;
/*     */ import org.apache.shiro.SecurityUtils;
/*     */ import org.apache.shiro.subject.Subject;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.stereotype.Service;
/*     */ 
/*     */ @Service("permission")
/*     */ public class PermissionService {
/*  21 */   private static final Logger log = LoggerFactory.getLogger(PermissionService.class);
/*     */   
/*     */   public static final String NOACCESS = "hidden";
/*     */   
/*     */   private static final String ROLE_DELIMETER = ",";
/*     */   
/*     */   private static final String PERMISSION_DELIMETER = ",";
/*     */   
/*     */   public String hasPermi(String permission) {
/*  38 */     return isPermitted(permission) ? "" : "hidden";
/*     */   }
/*     */   
/*     */   public String lacksPermi(String permission) {
/*  49 */     return isLacksPermitted(permission) ? "" : "hidden";
/*     */   }
/*     */   
/*     */   public String hasAnyPermi(String permissions) {
/*  60 */     return hasAnyPermissions(permissions, ",") ? "" : "hidden";
/*     */   }
/*     */   
/*     */   public String hasRole(String role) {
/*  71 */     return isRole(role) ? "" : "hidden";
/*     */   }
/*     */   
/*     */   public String lacksRole(String role) {
/*  82 */     return isLacksRole(role) ? "" : "hidden";
/*     */   }
/*     */   
/*     */   public String hasAnyRoles(String roles) {
/*  93 */     return isAnyRoles(roles, ",") ? "" : "hidden";
/*     */   }
/*     */   
/*     */   public boolean isUser() {
/* 103 */     Subject subject = SecurityUtils.getSubject();
/* 104 */     return (subject != null && subject.getPrincipal() != null);
/*     */   }
/*     */   
/*     */   public boolean isPermitted(String permission) {
/* 115 */     return SecurityUtils.getSubject().isPermitted(permission);
/*     */   }
/*     */   
/*     */   public boolean isLacksPermitted(String permission) {
/* 126 */     return (isPermitted(permission) != true);
/*     */   }
/*     */   
/*     */   public boolean hasAnyPermissions(String permissions) {
/* 137 */     return hasAnyPermissions(permissions, ",");
/*     */   }
/*     */   
/*     */   public boolean hasAnyPermissions(String permissions, String delimeter) {
/* 149 */     Subject subject = SecurityUtils.getSubject();
/* 151 */     if (subject != null) {
/* 153 */       if (delimeter == null || delimeter.length() == 0)
/* 155 */         delimeter = ","; 
/* 158 */       for (String permission : permissions.split(delimeter)) {
/* 160 */         if (permission != null && subject.isPermitted(permission.trim()) == true)
/* 162 */           return true; 
/*     */       } 
/*     */     } 
/* 167 */     return false;
/*     */   }
/*     */   
/*     */   public boolean isRole(String role) {
/* 178 */     return SecurityUtils.getSubject().hasRole(role);
/*     */   }
/*     */   
/*     */   public boolean isLacksRole(String role) {
/* 189 */     return (isRole(role) != true);
/*     */   }
/*     */   
/*     */   public boolean isAnyRoles(String roles) {
/* 200 */     return isAnyRoles(roles, ",");
/*     */   }
/*     */   
/*     */   public boolean isAnyRoles(String roles, String delimeter) {
/* 212 */     Subject subject = SecurityUtils.getSubject();
/* 213 */     if (subject != null) {
/* 215 */       if (delimeter == null || delimeter.length() == 0)
/* 217 */         delimeter = ","; 
/* 220 */       for (String role : roles.split(delimeter)) {
/* 222 */         if (subject.hasRole(role.trim()) == true)
/* 224 */           return true; 
/*     */       } 
/*     */     } 
/* 229 */     return false;
/*     */   }
/*     */   
/*     */   public Object getPrincipalProperty(String property) {
/* 240 */     Subject subject = SecurityUtils.getSubject();
/* 241 */     if (subject != null) {
/* 243 */       Object principal = subject.getPrincipal();
/*     */       try {
/* 246 */         BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
/* 247 */         for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
/* 249 */           if (pd.getName().equals(property) == true)
/* 251 */             return pd.getReadMethod().invoke(principal, (Object[])null); 
/*     */         } 
/* 255 */       } catch (Exception e) {
/* 257 */         log.error("Error reading property [{}] from principal of type [{}]", property, principal.getClass().getName());
/*     */       } 
/*     */     } 
/* 260 */     return null;
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\web\service\PermissionService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */